import _ from 'lodash'
import moment from 'moment'
import {query,fromTo} from '../services/attendance'
export default {
  namespace: 'attendance',
  state: {
      attendanceMap:{

      },
      selected:null,
      loading:false,
      fromToLoading:false,
      fromToData:null,
      fromToHeader:'',
      fromToStatus:{
          from:new moment().subtract(7,'days'),
          to:new moment(),
          tag:'一年级',
          week:1,
      }
  },
  reducers: {
      loading(state){
          return {...state,loading:true}
      },
      finishLoading(state){
          return {...state,loading:false}
      },
      update(state,{payload:{id,date,result}}){
          let dateKeys = result.map((v)=>{return new moment(v.Date).format('YYYY-M-D')});
          let r = _.zipObject(dateKeys,result);
          return {...state,attendanceMap:{
              ...state.attendanceMap,[id]:r
          }}
      },
      selectTeacher(state,{payload:{selected}}){
          return {...state,selected}
      },
      fromToLoading(state){
          return {...state,fromToLoading:true};
      },
      fromToFinish(state){
          return {...state,fromToLoading:false};
      },
      fromToEnd(state,{payload:{data}}){
          return {...state,fromToData:data};
      },
      fromToHeader(state,{payload:{fromToHeader}}){
          return {...state,fromToHeader};
      },
      fromToSearch(state,{payload}){
          return {...state,fromToStatus:{...state.fromToStatus,...payload}}
      }

  },
  effects: {
      *select({payload:{selected,needload}},{call,put}){
          if(needload){
              yield put({type:'loading'});
            //   yield put({type:selectTeacher,payload:{selected}});
              let result = yield call(query,{id:selected.id});
            //   console.log(result);
              if(result.success){
                  yield put({type:'update',payload:{
                      id:selected.id,result:result.data
                  }})
              }
              yield put({type:'finishLoading'});
              yield put({type:'selectTeacher',payload:{selected}});

          }else{
              yield put({type:'selectTeacher',payload:{selected}});
          }
      },
      *fromTo({payload:{from,to,tag}},{call,put}){
       let _from = from.format('YYYY-M-D');
       let _to = to.format('YYYY-M-D');
    //    console.log(from,to,tag);
        yield put({type:'fromToLoading'});
        let result = yield call(fromTo,_from,_to,tag);
        console.log(result)
        if(result.success){
            let data = result.data;
            data = _.reduce(data,(r,p)=>{
                let {name,Date,morning,afternoon} = p;
                if(!r[name]){
                    r[name]={
                        morning:{
                            ok:0,
                            null:0,
                            error:0
                        },
                        afternoon:{
                            ok:0,
                            null:0,
                            error:0
                        }
                    }
                }
                let t = r[name];
                // let xiaoxue = true;
                // if(['七年级','八年级','九年级'].includes(tag)){
                //     xiaoxue=false;
                // }
                if(morning){
                    if(new moment(morning,'hh:mm:ss')> new moment('08:30:00','hh:mm:ss')){
                        t.morning.error++;

                    }else{
                        t.morning.ok++;
                    }
                }else{
                    t.morning.null++;
                }
                let day = new moment(Date).day();
                // console.log(day);
                if(afternoon){

                    if(day == 1 || day == 5){

                        if(new moment(afternoon,'hh:mm:ss') < new moment('15:40:00','hh:mm:ss')){
                            t.afternoon.error++;

                        }else{
                            t.afternoon.ok++;
                        }
                    }else{

                        if(new moment(afternoon,'hh:mm:ss') < new moment('16:40:00','hh:mm:ss')){
                            t.afternoon.error++;

                        }else{
                            t.afternoon.ok++;
                        }
                    }
                    // if(xiaoxue){

                    // }else{

                    // }
                }else{
                    t.afternoon.null++;
                }
                return r;
            },{})
          data = _.reduce(data,(r,{morning,afternoon},k)=>{
            r.push({
                  name:k,
                  morning,
                  afternoon,
              })
            return r;
          },[]);
          console.log(data);
          yield put({type:'fromToHeader',payload:{fromToHeader:from.format('YYYY年MM月DD日')+' --- '+to.format('YYYY年MM月DD日   ') + tag}})
          yield put({type:'fromToEnd',payload:{data}});

        }
        yield put({type:'fromToFinish'});

      }
  },
  subscriptions: {},
};
